public class ParticleEngine {
  // alist to keep track of particles
  private ArrayList<Particle> particlesAc;
  private ArrayList particlesAcs, particlesDir;
  private ArrayList springs;
  
  //the class constructor
  public ParticleEngine() {
    //create the list
    particlesAc = new ArrayList();
    particlesAcs = new ArrayList();
    particlesDir = new ArrayList();
    springs = new ArrayList();
  }
  
  public void addParticle(Particle particle, int i) {
    if(i==0){ //ac
      if(particlesAc==null){
        particlesAc = new ArrayList<Particle>();
        //particlesAc.add(particle);
      }
      //else if(findParticle(particle.label, i)!=null)
        particlesAc.add(particle);
    }
    else if (i==1){ //acs
      if(particlesAcs==null){
        particlesAcs = new ArrayList<Particle>();
        //particlesAcs.add(particle);
      }
      //else if(findParticle(particle.label, i)!=null)
        particlesAcs.add(particle);
    }
    else if (i==2){ //dir
      if(particlesDir==null){
        particlesDir = new ArrayList<Particle>();
        //particlesDir.add(particle);
      }
      //else if(findParticle(particle.label, i)!=null)
        particlesDir.add(particle);
    }
    //public function to add a particle
    particle.setEngine(this);
    
  }
  
  public void addSpring(Spring spring) {
    if(springs==null) springs = new ArrayList<Spring>();
    //public function to add a particle
    spring.setEngine(this);
    
    springs.add(spring);
  }
  
  //update function to update all springs and particles
  public void update() {
    
    //use a while loop to check all particles and springs
    /*int i = 0;
    while (i<springs.size()) {
      //update it
      Spring s = (Spring) springs.get(i);
      s.update();
      i++;
    }
    i=0;
     while (i<particles.size()) {
      //update it
      Particle p = (Particle) particles.get(i);
      p.update();
      i++;
    }*/
 
  }
  
  //draw all particles
  public void draw() {
    
    for (int i=0; i<springs.size(); i++) {
      Spring s = (Spring) springs.get(i);
      s.draw();
    }
    for (int i=0; i<particlesAc.size(); i++) {
      Particle p = (Particle) particlesAc.get(i);
      p.draw();
    }
    for (int i=0; i<particlesAcs.size(); i++) {
      Particle p = (Particle) particlesAcs.get(i);
      p.draw();
    }
    for (int i=0; i<particlesDir.size(); i++) {
      Particle p = (Particle) particlesDir.get(i);
      p.draw();
    }
  }
 /* 
  public Particle particleAt(float x, float y) {
    int i = particles.size();
    while(i>0) {
      i--;
      Particle p = (Particle) particles.get(i);
      float dx = p.x - x;
      float dy = p.y - y;
      if (dx * dx + dy * dy < p.size * p.size) {
        return p;
      }
    }
    return null;
  }*/
  
  public Particle findParticle(String label, int k) {
    int i ;
    if (k==0){
    i = particlesAc.size();
    while(i>0) {
      i--;
      Particle p = (Particle) particlesAc.get(i);
      if (p.label == label) {
        return p;
      }
    }
    }
     if (k==1){
    i = particlesAcs.size();
    while(i>0) {
      i--;
      Particle p = (Particle) particlesAcs.get(i);
      if (p.label == label) {
        return p;
      }
    }
    }
     if (k==2){
    i = particlesDir.size();
    while(i>0) {
      i--;
      Particle p = (Particle) particlesDir.get(i);
      if (p.label == label) {
        return p;
      }
    }
    }
    return null;
  }
  
  boolean findSpring(Particle start, Particle end) {
    int i = springs.size();
    while(i>0) {
      i--;
      Spring p = (Spring) springs.get(i);
      if( (p.start.label.equals(start.label) && p.end.label.equals(end.label)) 
            || 
          (p.start.label.equals(end.label) && p.end.label.equals(start.label)) ){
        return true;
      }
    }
    return false;
  }
  
  public void connectParticles(String label1, String label2, int b, int e) {
    Particle particle1 = findParticle(label1, b);
    Particle particle2 = findParticle(label2, e);
    if (particle1 != null && particle2 != null){
        Spring s = new Spring(particle1, particle2, 60, 0.02);
        s.appear=true;
        particle2.enlighted=true;
        addSpring(s);   
    }
  }
  
  public void clearAll(){
    if(particlesAc!=null)  particlesAc=null;
    if(particlesAcs!=null)  particlesAcs=null;
    if(particlesDir!=null)  particlesDir=null;
    if(springs!=null)  springs=null;
    return;
  }
  
}
